package com.dubbo.provider.consumer;

import com.dubbo.provider.entity.Invocation;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;


/**
 * @description: 发送请求
 * @author: 石亚宁
 * @create: 2020-05-28 16:42
 **/

public class HttpClientLocal {
	public String send(String hostName,int port, Invocation invocation) throws IOException {
		URL url=new URL("http",hostName,port,"/");
		HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
		urlConnection.setRequestMethod("POST");
		urlConnection.setDoOutput(true);

		OutputStream outputStream = urlConnection.getOutputStream();
		ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);
		objectOutputStream.writeObject(invocation);

		objectOutputStream.flush();
		objectOutputStream.close();
		InputStream inputStream = urlConnection.getInputStream();
		return IOUtils.toString(inputStream);

	}
}
